package com.snakegame.ui;

import com.snakegame.core.GameManager;
import com.snakegame.config.GameState;
import com.snakegame.config.Map;
import com.snakegame.config.MapManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * 地图选择面板，允许用户选择游戏地图
 */
public class MapSelectionPanel extends JPanel {
    private GameManager gameManager;
    private GameFrame gameFrame;
    
    public MapSelectionPanel(GameManager gameManager, GameFrame gameFrame) {
        this.gameManager = gameManager;
        this.gameFrame = gameFrame;
        
        setLayout(new BorderLayout());
        setBackground(Color.BLACK);
        
        // 标题
        JLabel titleLabel = new JLabel("选择地图");
        titleLabel.setFont(new Font("宋体", Font.BOLD, 24));
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        add(titleLabel, BorderLayout.NORTH);
        
        // 地图选择区域
        JPanel mapsPanel = new JPanel();
        mapsPanel.setLayout(new GridLayout(0, 3, 10, 10));
        mapsPanel.setBackground(Color.BLACK);
        mapsPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        // 添加地图按钮
        List<Map> maps = MapManager.getMaps();
        ButtonGroup mapGroup = new ButtonGroup();
        
        for (Map map : maps) {
            JRadioButton mapButton = new JRadioButton(map.getName());
            mapButton.setFont(new Font("宋体", Font.PLAIN, 16));
            mapButton.setForeground(Color.WHITE);
            mapButton.setBackground(Color.BLACK);
            mapButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    gameManager.setCurrentMap(map);
                }
            });
            
            // 如果是当前选中的地图，默认选中
            if (map == gameManager.getCurrentMap()) {
                mapButton.setSelected(true);
            }
            
            mapGroup.add(mapButton);
            mapsPanel.add(mapButton);
        }
        
        JScrollPane scrollPane = new JScrollPane(mapsPanel);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        scrollPane.getViewport().setBackground(Color.BLACK);
        add(scrollPane, BorderLayout.CENTER);
        
        // 底部按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.setBackground(Color.BLACK);
        
        // 返回按钮
        JButton backButton = new JButton("返回");
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameManager.setGameState(GameState.START_MENU);
                gameFrame.showPanel("START_MENU");
            }
        });
        buttonPanel.add(backButton);
        
        // 确定按钮
        JButton confirmButton = new JButton("确定");
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameManager.setGameState(GameState.START_MENU);
                gameFrame.showPanel("START_MENU");
            }
        });
        buttonPanel.add(confirmButton);
        
        add(buttonPanel, BorderLayout.SOUTH);
    }
}    